﻿<!DOCTYPE html>
<html>
<head>
    <title data-i18n="auth-title">ASF | Authentication</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script> <!--Change to cdnjs once available-->
    <link rel="icon" href="img/favicon.png" type="image/png">
    <link rel="shortcut icon" href="img/favicon.ico" type="img/x-icon">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/admin-lte/2.4.3/css/AdminLTE.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.i18n/1.0.4/jquery.i18n.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.i18n/1.0.4/jquery.i18n.messagestore.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.i18n/1.0.4/jquery.i18n.fallbacks.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.i18n/1.0.4/jquery.i18n.language.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.i18n/1.0.4/jquery.i18n.parser.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.i18n/1.0.4/jquery.i18n.emitter.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.i18n/1.0.4/jquery.i18n.emitter.bidi.min.js"></script>
    <script src="../js/utils.js"></script>
    <script src="../js/i18n.js"></script>
</head>
<body class="hold-transition lockscreen">
    <div class="lockscreen-wrapper">
        <div class="lockscreen-logo">
            <a href="index.html"><b>Archi</b>SteamFarm</a>
        </div>
        <div class="lockscreen-name" data-i18n="auth-ipc-header">IPC password required</div>

        <div class="lockscreen-item">
            <div class="lockscreen-image">
                <img src="img/favicon.png" alt="ASF">
            </div>
            <form class="lockscreen-credentials">
                <div class="input-group">
                    <input type="password" id="IPCPassword" class="form-control" data-i18n="placeholder-auth" placeholder="Type your password...">

                    <div class="input-group-btn">
                        <button type="button" id="TryToAuth" class="btn"><i class="fa fa-arrow-right text-muted"></i></button>
                    </div>
                </div>
            </form>
        </div>
        <div class="help-block text-center">
            <span data-i18n="auth-ipc-text">Please enter the correct IPC password</span>
        </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/admin-lte/2.4.3/js/adminlte.min.js"></script>
    <script>
            $(function () {
                'use strict';

                var tmpIsAuthorized = get('IsAuthorized'),
                    tmpLastPage = get('CurrentPage'),
                    redirectPage = 'pages/commands.html';

                if (tmpLastPage) {
                    redirectPage = tmpLastPage;
                }

                if (tmpIsAuthorized === 'true') {
                    window.location.replace(redirectPage);
                } else {
                    $.ajax({
                        url: '/Api/ASF',
                        type: 'GET',
                        statusCode: {
                            401: function () {
                                store('IsAuthorized', false);
                                store('IPCPassword', '');
                            },
                            403: function () {
                                store('IsAuthorized', false);
                                store('IPCPassword', '');
                                $('.lockscreen-name, .lockscreen-item').remove();
                                var res = $.i18n('auth-ipc-403');
                                $('.help-block').text(res);
                            },
                            200: function () {
                                store('IsAuthorized', true);
                                window.location.replace(redirectPage);
                            }
                        }
                    });
                }

                $(document).keypress(function (e) {
                    var keyCode = e.keyCode || e.which;

                    if (keyCode == 13) {
                        e.preventDefault();
                        $('#TryToAuth').click();
                    }
                });

                $('#TryToAuth').click(function () {
                    var newPwd = $('#IPCPassword').val();

                    if (newPwd === false) return false;

                    if (newPwd === '') {
                        swal({
                            title: $.i18n('global-error-title'),
                            text: $.i18n('global-error-invalid-pw'),
                            type: 'error'
                        }, function () { swal.close(); });
                        return false;
                    }

                    $.ajax({
                        url: '/Api/ASF',
                        type: 'GET',
                        beforeSend: function (jqXHR) {
                            jqXHR.setRequestHeader('Authentication', newPwd);
                        },
                        statusCode: {
                            401: function () {
                                store('IsAuthorized', false);

                                swal({
                                    title: $.i18n('global-error-title'),
                                    text: $.i18n('auth-ipc-pw-wrong'),
                                    type: 'error'
                                }, function () { swal.close(); });
                            },
                            403: function () {
                                store('IsAuthorized', false);
                                $('.lockscreen-name, .lockscreen-item').remove();
                                $('.help-block').text($.i18n('auth-ipc-403'));
                            },
                            200: function () {
                                store('IsAuthorized', true);
                                store('IPCPassword', newPwd);
                                
                                swal({
                                    title: $.i18n('global-success-title'),
                                    text: $.i18n('auth-ipc-pw-saved'),
                                    type: 'success'
                                }, function () { window.location.replace(redirectPage); });
                            }
                        }
                    });
                });
            })
    </script>
</body>
</html>
